﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.View;

namespace Benefit.Service.View
{
    /// <summary>
    /// 交易员日多空数据
    /// </summary>
    public class ServiceOperatorDayDirectionInfo : Benefit.Interface.View.IOperatorDayDirectionInfo
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorDayDirectionInfo(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        public OperatorDayDirectionInfo GetOperatorDayDirectionInfo(int tradeHistoryId, int operatorid, bool win)
        {
            OperatorDayDirectionInfo data = new OperatorDayDirectionInfo();
            if (win)
            {
                data.Datas = db.OperatorDayDirectionInfoData.Where(a => a.OperatorId == operatorid).Where(a => a.SumValue > 0).Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
            }
            else
            {
                data.Datas = db.OperatorDayDirectionInfoData.Where(a => a.OperatorId == operatorid).Where(a => a.SumValue <= 0).Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
            }
            return data;
        }



        public void InitOperatorDayDirectionInfo(int tradeHistoryId, bool win)
        {

            var operators = db.Operators.ToList();
            foreach (Models.Sys.Operator op in operators)
            {
                OperatorDayDirectionInfo info = new OperatorDayDirectionInfo();
                info.Datas = new List<OperatorDayDirectionInfoData>();
                Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
                Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
                List<Models.Sys.Account> accounts = idoa.GetDayOperatorAccounts(ith.GetDateTimeFromTradeHistoryId(tradeHistoryId), op.Id);
                foreach (Models.Sys.Account account in accounts)
                {
                    Interface.Day.IAccountDayTradeProduct iadtp = new Day.ServiceAccountDayTradeProduct(db);
                    List<Models.Day.AccountDayTradeProduct> infos = iadtp.GetAccountDayTradeProduct(account.Id, tradeHistoryId, win);
                    foreach (Models.Day.AccountDayTradeProduct i in infos)
                    {
                        List<Models.View.OperatorDayDirectionInfoData> ps = null;
                        if (i.Direction == 1)
                        {
                            ps = info.Datas.Where(a => a.Direction.Equals("空头")).ToList();
                        }
                        else
                        {
                            ps = info.Datas.Where(a => a.Direction.Equals("多头")).ToList();
                        }
                        if (ps.Count > 0)
                        {
                            OperatorDayDirectionInfoData data = ps.First();
                            data.BillCount = data.BillCount + i.BillCount;
                            //   data.SumValue = data.SumValue + Math.Abs(i.Profit);
                            data.SumValue = data.SumValue + i.Profit;
                        }
                        else
                        {
                            OperatorDayDirectionInfoData data = new OperatorDayDirectionInfoData();
                            if (i.Direction == 1)
                            {
                                data.Direction = "空头";
                            }
                            else
                            {
                                data.Direction = "多头";
                            }

                            data.BillCount = i.BillCount;
                            //   data.SumValue = Math.Abs(i.Profit);
                            data.SumValue = i.Profit;
                            info.Datas.Add(data);
                        }
                    }
                }
                var allSum = info.Datas.Sum(a => a.SumValue);
                foreach (OperatorDayDirectionInfoData a in info.Datas)
                {
                    a.Percent = Convert.ToInt32((a.SumValue / allSum) * 100);
                    a.OperatorId = op.Id;
                    a.TradeHistoryId = tradeHistoryId;
                    db.OperatorDayDirectionInfoData.Add(a);
                }

            }
            db.SaveChanges();
        }

        public void Delete(int tradeHistoryId)
        {
            var query = db.OperatorDayDirectionInfoData.Where(a => a.TradeHistoryId == tradeHistoryId);
            foreach (Models.View.OperatorDayDirectionInfoData d in query)
            {
                db.OperatorDayDirectionInfoData.Remove(d);
            }
            db.SaveChanges();
        }
    }
}
